rm(list = ls())

# packages
library(dplyr)
library(ggplot2)
library(rio)
library(ggpubr) 

# data
data <- import("data/ReplicationData_Bornschieretal_CPS.dta")


# rename variables

data <- dplyr::rename(data, 
                      g1university=C04_1_C04_scale, g2apprenticeship=C04_2_C04_scale, g3noapprenticeship=C04_3_C04_scale, g4wealthy=C04_4_C04_scale, g5middleclass=C04_5_C04_scale, 
                      g6modestmeans=C04_6_C04_scale, g7similarjob=C04_7_C04_scale, g8withpeople=C04_8_C04_scale, g9product=C04_9_C04_scale, g10migration=C04_10_C04_scale,
                      g11urban=C04_11_C04_scale, g12rural=C04_12_C04_scale, g13cosmopolitan=C04_13_C04_scale, g14men=C04_14_C04_scale, g15women=C04_15_C04_scale, 
                      g16cultured=C04_16_C04_scale, g17swiss=C04_17_C04_scale, 
                      party=E01) 

data[data==999] <- NA 

# Create voter group variable
table(data$party)

data$votelrc <- NA
data$votelrc[data$party == 3 | data$party == 5] <- "new left"
data$votelrc[data$party <=2 | data$party == 7] <- "center(-right)"
data$votelrc[data$party == 4] <- "far right"
table(data$votelrc)


# Figure 3: Plot ID-divergences by party electorate ---------------------------------


df <- subset(data, select=c(g1university,g2apprenticeship, g3noapprenticeship, g4wealthy, g5middleclass, g6modestmeans, g7similarjob, g8withpeople, g9product, g10migration, g11urban, g12rural,
                            g13cosmopolitan, g14men, g15women, g16cultured, g17swiss, votelrc))

df <- dplyr::rename(df, University=g1university, Apprenticeship=g2apprenticeship, "No_apprenticeship"=g3noapprenticeship, Wealthy=g4wealthy, "Middle_class"=g5middleclass,
                    "Of_modest_means"=g6modestmeans, "With_a_similar_job"=g7similarjob, "Job_working_with_people"=g8withpeople, "Job_producing_a_product"= g9product, "Migrant_background"=g10migration,
                    Urban=g11urban, Rural=g12rural, Cosmopolitan= g13cosmopolitan, Men=g14men, Women=g15women, Cultured=g16cultured, Swiss=g17swiss, Electorate = votelrc)


gd <- df %>% 
  group_by(Electorate) %>% 
  summarise_all(funs(mean), na.rm=TRUE) %>% 
  filter(!is.na(Electorate))

gd2 <- df %>% 
  summarise_all(funs(mean), na.rm=TRUE) 

vars = colnames(df)[-length(df)] 

gd.new = gd
plots = list()
for(var in vars){
  sm = gd2[[var]] # sample mean
  gd.new[[var]] = gd[[var]] - sm # deviation of party means from sample mean
  p = ggplot(gd.new, aes_string("Electorate", var, fill=("Electorate"))) + 
    geom_bar(stat="identity") +
    theme_classic() + 
    coord_flip()  +
    scale_fill_grey() + 
    geom_hline(aes(yintercept=0)) +
    scale_y_continuous(breaks = c(-0.5, -0.25, 0, 0.25, 0.5),
                       labels = c(round(sm - 0.5, digits = 2), round(sm - 0.25, digits = 2), round(sm, digits = 2), round(sm + 0.25, digits = 2), round(sm + 0.5, digits = 2)),
                       limit = c(-0.5,0.5)) + 
    theme(axis.text.y=element_blank(), axis.title.y=element_blank(), axis.line.y=element_blank(), axis.ticks.y = element_blank(), plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm")) + 
    theme(legend.title = element_blank())
  plots[[var]] = p
}

fig3 <- ggarrange(plots[[1]], plots[[2]], plots[[3]], plots[[4]], plots[[5]], plots[[6]], 
          plots[[7]], plots[[8]], plots[[9]], plots[[10]], plots[[11]], plots[[12]], 
          plots[[13]], plots[[14]], plots[[15]], plots[[16]], plots[[17]],
          common.legend = TRUE, legend = "bottom", ncol = 4, nrow= 5)

ggsave("output/figure3.pdf",  fig3, height=6.5, width=8)
